//
//  GenerateRoseCell.m
//  ROSE
//
//  Created by 艾阳 on 2018/5/31.
//  Copyright © 2018年 HeNanZhongDunYunAn. All rights reserved.
//

#import "GenerateRoseCell.h"

@implementation GenerateRoseCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.isSelected = NO;//默认没有选中
        self.backgroundColor = [UIColor colorWithRed:239/255.0 green:240/255.0 blue:241/255.0 alpha:1];
        if (!_imgView) {
            _imgView = [[UIImageView alloc]init];
            _imgView.contentMode = UIViewContentModeScaleAspectFit;
            _imgView.backgroundColor = [UIColor colorWithRed:255/255.0 green:228/255.0 blue:225/255.0 alpha:1];
            //255,228,225
            [self.contentView addSubview:_imgView];
        }
        
        if (!_name) {
            _name = [[UILabel alloc]init];
            _name.textColor = [UIColor blackColor];
            _name.font = [UIFont systemFontOfSize:13];
            _name.backgroundColor = [UIColor clearColor];
            _name.text = @"粉红回忆";
            [self.contentView addSubview:_name];
        }
        
        if (!_count) {
            _count = [[UILabel alloc]init];
            _count.font = [UIFont systemFontOfSize:10];
            _count.backgroundColor = [UIColor clearColor];
            _count.text = @"剩余658朵";
            _count.textAlignment = NSTextAlignmentRight;
            [self.contentView addSubview:_count];
        }
       
        [self makeLayout];
    }
    return self;
}


- (void)makeLayout{
    
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        //make.edges.mas_equalTo(UIEdgeInsetsMake(10, 10, 50, 10));
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(10);
        make.height.mas_equalTo(((size_width-85)/2-20)*380.0/515.0);
    }];
    
    
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.imgView.mas_left);
        make.top.mas_equalTo(self.imgView.mas_bottom).with.offset(15.0f);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(16);
    }];
    
    [self.count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.imgView.mas_right);
        make.top.mas_equalTo(self.imgView.mas_bottom).with.offset(15.0f);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(16);
    }];
    
  
}

- (void)setDataWithGenerateModel:(GenerateModel *)generateModel{
    NSString *imgName =[NSString stringWithFormat:@"rose-%ld",generateModel.LX.integerValue+1];
    [self.imgView setImage:[UIImage imageNamed:imgName]];
    self.name.text = generateModel.LXMC;
    [self.count setAttributedText: [self changeLabelWithText:[NSString stringWithFormat:@"剩余%@朵",generateModel.SY]]];
}

//创建一个返回富文本的方法
-(NSMutableAttributedString*) changeLabelWithText:(NSString*)needText

{
    
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:needText];
    

    [attrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,2)];
    [attrString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2,needText.length-3)];
    [attrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(needText.length-1,1)];
    return attrString;
    
}


// 64base字符串转图片

- (UIImage *)stringToImage:(NSString *)str {
    
    NSData * imageData =[[NSData alloc] initWithBase64EncodedString:str options:NSDataBase64DecodingIgnoreUnknownCharacters];
    
    UIImage *photo = [UIImage imageWithData:imageData ];
    
    return photo;
    
}
@end
